function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}
layui.use(['form', 'layedit', 'laydate'], function(){
    var form = layui.form
        ,layer = layui.layer
        ,layedit = layui.layedit
        ,laydate = layui.laydate;
    var url = "/general/erp5/public/tripapi/plan/Save";
    var urledit = "/general/erp5/public/tripapi/plan/update";
    var approveurl = "/general/erp5/public/tripapi/plan/TravelApprove";

    //日期
    laydate.render({
        elem: '#date'
    });
    laydate.render({
        elem: '#date1'
    });

    //自定义验证规则
    form.verify({
        title: function(value){
            if(value.length == 0){
                return '请输入填报部门';
            }
        },
        people: function(value){
            if(value.length == 0){
                return '请输入填报人';
            }
        },
        content: function(value){
            layedit.sync(editIndex);
        }
    });

    //提交
    form.on('submit(demo1)', function(data){
        $.ajax({
            type: "POST",
            url: url,
            data: data.field,
            traditional: true,
            dataType:"json",
            success: function(returnData){
                if(returnData.status == 200){
                    $.get("/general/erp5/public/common.php?opt=send_msg&params="+returnData.data.params, function(result){
                        layer.open({
                            type: 1
                            ,offset: 'auto'
                            ,id: 'layerDemo'
                            ,content: '<div style="padding: 20px 100px;">'+ returnData.msg +'</div>'
                            ,btn: '确定'
                            ,btnAlign: 'c'
                            ,shade: 0
                            ,yes: function(){
                                layer.closeAll();
                            }
                        });
                    });
                }else{
                    layer.open({
                        type: 1
                        ,offset: 'auto'
                        ,id: 'layerDemo'
                        ,content: '<div style="padding: 20px 100px;">'+ returnData.msg +'</div>'
                        ,btn: '确定'
                        ,btnAlign: 'c'
                        ,shade: 0
                        ,yes: function(){
                            layer.closeAll();
                        }
                    });
                }
            }
        })
        return false;
    });

    form.on('submit(demo2)', function(data){
        $.ajax({
            type: "POST",
            url: urledit,
            data: data.field,
            traditional: true,
            dataType:"json",
            success: function(returnData){
                console.log(returnData);
                if(returnData.status == 200){
                    layer.open({
                        type: 1
                        ,offset: 'auto'
                        ,id: 'layerDemo'
                        ,content: '<div style="padding: 20px 100px;">'+ returnData.msg +'</div>'
                        ,btn: '确定'
                        ,btnAlign: 'c'
                        ,shade: 0
                        ,yes: function(){
                            layer.closeAll();
                        }
                    });
                }else{
                    layer.open({
                        type: 1
                        ,offset: 'auto'
                        ,id: 'layerDemo'
                        ,content: '<div style="padding: 20px 100px;">'+ returnData.msg +'</div>'
                        ,btn: '确定'
                        ,btnAlign: 'c'
                        ,shade: 0
                        ,yes: function(){
                            layer.closeAll();
                        }
                    });
                }
            }
        })
        return false;
    });

    form.on('submit(demo3)', function(data){
        $.ajax({
            type: "POST",
            url: approveurl+'?approvestatus=true&id='+getQueryString('id'),
            data: data.field,
            traditional: true,
            dataType:"json",
            success: function(returnData){
                console.log(returnData);
                if(returnData.status == 200){
                    layer.open({
                        type: 1
                        ,offset: 'auto'
                        ,id: 'layerDemo'
                        ,content: '<div style="padding: 20px 100px;">'+ returnData.msg +'</div>'
                        ,btn: '确定'
                        ,btnAlign: 'c'
                        ,shade: 0
                        ,yes: function(){
                            layer.closeAll();
                        }
                    });
                }else{
                    layer.open({
                        type: 1
                        ,offset: 'auto'
                        ,id: 'layerDemo'
                        ,content: '<div style="padding: 20px 100px;">'+ returnData.msg +'</div>'
                        ,btn: '确定'
                        ,btnAlign: 'c'
                        ,shade: 0
                        ,yes: function(){
                            layer.closeAll();
                        }
                    });
                }
            }
        })
        return false;
    });

    // $("#demo3").off('click').on('click',function(){
    //     var id = getQueryString('id');
    //     console.log(id);
    // })

});